/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.widget;

import android.content.Context;
import com.mol.market.R;

import java.io.Serializable;
import java.util.Random;


/**
 * 角标
 * @author Chaos	
 * @date 2013-1-29 
 */
public class CornerMark implements Serializable {
    /**
     * @Fields serialVersionUID 
     */
    private static final long serialVersionUID = -6305209382174192431L;

    /**
     * @unused
     */
    private String mUrl;
    
    private Marks mMark;
    
    public static CornerMark createEmptyCorner(){
        return new CornerMark(null, Marks.NOTHING);
    }
    
    /**
     * 生成指定角标
     * @param mark
     */
    public CornerMark(Marks mark){
        this.mMark = mark;
    }

    /**
     * 生成指定角标
     * @param mark
     */
    public CornerMark(int mark){

        switch (mark) {
            case 4:{
                this.mMark = Marks.DEBUT;
                break;
            }
            case 3:{
                this.mMark = Marks.RECOMMENDATION;
                break;
            }
            case 2:{
                this.mMark = Marks.LOCALIZATION;
                break;
            }
            case 1:{
                this.mMark = Marks.GREEN;
                break;
            }
        }
    }

    /**
     * 生成随即角标
     * @param ran
     */
    public CornerMark(Random ran){
        int length = Marks.values().length;
        this.mMark = Marks.values()[ran.nextInt(length)];
    }
    
    /**
     * @hide
     * @unused
     */
    public CornerMark(String url,Marks mark){
        this.mUrl  = url;
        this.mMark = mark;
    }
    
    /**
     * @hide
     * @unused
     */
    public CornerMark(String url,Random ran){
        this.mUrl  = url;
        int length = Marks.values().length;
        this.mMark = Marks.values()[ran.nextInt(length)];
    }
    
    public boolean isEmpty(){
        return mMark == null || mMark == Marks.NOTHING;
    }
    /**
     * @unused
     */
    public String getUrl(){
        return mUrl;
    }
    
    public int getResId(){
        return mMark.getResId();
    }
    
    @Override
    public String toString() {
        return mMark == null?"NOTHING":mMark.toString();
    }
    
    /**
     * 冒泡排序（从小到大）
     * @param ascending 如果为true，则表示正序 -> 0.1.2.3.4
     *                  如果为false，则表示逆序 -> 4.3.2.1.0
     */
    public static Marks[] bubbleSort(boolean ascending,Marks... marks) {
        int length = marks.length;
        for (int i = 0; i < length - 1; i++) {
            for (int j = i + 1; j < length; j++) {
                if(ascending){
                    if (marks[i].getLevel() > marks[j].getLevel()) {
                        Marks temp = marks[i];
                        marks[i] = marks[j];
                        marks[j] = temp;
                    }  
                }else{
                    if (marks[i].getLevel() < marks[j].getLevel()){
                        Marks temp = marks[i];
                        marks[i] = marks[j];
                        marks[j] = temp;
                    }
                }
                
            }
        }
        return marks;
    }
    
//    public static Marks getTop(Marks... marks){
//        return bubbleSort(false, marks)[0];
//    }
//    
//    public static Marks getLast(Marks... marks){
//        return bubbleSort(true, marks)[0];
//    }
    
    public enum Marks{
        
        //Marks( ResId , Description'string or id' , level)
        
        NOTHING(0, "NOTHING", 0),
        /**
         * 绿色
         */
        GREEN(R.drawable.flag_corner_green, R.string.label_clean, 1),
        /**
         * 汉化
         */
        LOCALIZATION(R.drawable.flag_corner_localization, 
                R.string.label_localized, 2),
        /**
         * 推荐
         */
        RECOMMENDATION(R.drawable.flag_corner_recommendation, 
                R.string.label_recommend, 3),
        /**
         * 首发
         */
        DEBUT(R.drawable.flag_corner_debut, R.string.label_first, 4);
        /**
         * 资源Id -> (R.drawable.xxx)
         */
        private int id;
        /**
         * 描述资源的文字
         */
        private String str;
        /**
         * 描述资源的文字的Id -> (string.xml)
         */
        private int strId;
        /**
         * 重要程度 数字越大表示越重要
         * 
         * 0.9.7的规则为 "首精汉绿"
         */
        private int level = -1;
        
        Marks(int id,String str,int level){
            this.id  = id;
            this.str = str;
            this.level = level;
        }
        
        Marks(int id,int strId,int level){
            this.id = id;
            this.strId = strId;
            this.level = level;
        }
        
        public int getLevel(){
            return level;
        }
        
        public int getResId(){
            return id;
        }
        
        public String getDescription(Context context){
            if(str != null){
                return str;
            }
            return context.getResources().getString(strId);
        }
        
        @Override
        public String toString() {
            StringBuffer sb = new StringBuffer();
            sb.append("name : " + super.toString());
            sb.append(" level : " + getLevel());
            sb.append(" resId : " + getResId());
            sb.append(" description : " + str == null?strId:str);
            return sb.toString();
        }
        
    }
}
